package top.kaoshanji.p1ch04a.support;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Enumeration;
import java.util.ResourceBundle;
import java.util.Vector;

/**
 * 自定义的资料包实现
 * author kaoshanji
 * ^_^
 * create time 2025/1/15 18:02
 */
public class ReflectiveResourceBundle extends ResourceBundle {

    private Class clazz;

    public ReflectiveResourceBundle(Class clazz) {
        this.clazz = clazz;
    }

    @Override
    protected Object handleGetObject(String key) {
        if (key == null) {
            throw new NullPointerException();
        }

        try {
            Method method = clazz.getMethod(key);
            if (method == null) {
                return null;
            }
            return method.invoke(null);

        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
            return null;
        }

    }

    @Override
    public Enumeration<String> getKeys() {
        Vector<String> result = new Vector<>();
        Method[] methods = clazz.getMethods();
        for (Method method: methods) {
            int mod = method.getModifiers();
            if (Modifier.isStatic(mod) && Modifier.isPublic(mod) && method.getParameterTypes().length == 0) {
                result.add(method.getName());
            }
        }

        return result.elements();
    }
}
